<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
	exit ('Access Denied');
}

/**
 * 管理员帐号业务逻辑类
 * 
 * 每个 Logic 对应一个业务逻辑处理单元、必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class AccountLogic extends TinBaseLogic {

	private $accountDao;
	private $roleDao;
	private $permissionDao;

	// 每个 Logic 需要有一个构造函数
	public function AccountLogic() {        
		parent::__construct();
		$this->logger->className = "AccountLogic";
		$this->accountDao = Tin::getDAO("AssignTable","ACCOUNT");
		$this->roleDao = Tin::getDAO("AssignTable","ROLE");
		$this->permissionDao = Tin::getDAO("AssignTable","PERMISSION");
	}
	
	public function __destruct(){
		$this->accountDao = null;
		$this->roleDao = null;
		$this->permissionDao = null;
	}
//=========================================用户=============================================================
	/** 获取指定记录 */
	public function getAccountItem($cols, $cond){
		return $this->accountDao->selectRowByCond($cols, $cond);
	}
	/** 根据指定条件获取分页列表数据 */
	public function getAccountList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->accountDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->accountDao->getCountByCond($cond);
		}
		return $res;
	}
	/** 根据指定条件更新记录 */
	public function updateByCond($data, $cond){
        try{
            $this->beginTrans(); //开启事务
            
            $map = Tin :: createDataMap();
            $map->addEntry('USERNAME', $data['username'], DB::VARCHAR);
            $map->addEntry('PASSWORD', $data['password'], DB::VARCHAR);
            $map->addEntry('REAL_NAME', $data['real_name'], DB::VARCHAR);
            $map->addEntry('EMALL', $data['emall'], DB::VARCHAR);
            $map->addEntry('TEL', $data['tel'], DB::VARCHAR);
            $map->addEntry('MOBILE', $data['mobile'], DB::VARCHAR);
            $map->addEntry('STATE', $data['state'], DB::INT);
            $map->addEntry('ROLE_ID', $data['role_id'], DB::INT);
            $map->addEntry('SKIN_NAME', $data['skin_name'], DB::VARCHAR);
            $res = $this->accountDao->updateByCond($map, $cond, false);
            
            $this->endTrans(); //结束并提交事务
            return $res;
        } catch(Exception $e){
            $this->logger->error("updateByCond:".$e->getMessage());
            $this->giveUpTrans();   //事务失败回滚
            return false;
        }
	}
	/** 根据条件更新指定字段 */
	public function updateFieldVal($fieldN, $fieldV, $cond){
		$map = Tin :: createDataMap();
		$map->addEntry($fieldN, $fieldV, DB::VARCHAR);
		$res = $this->accountDao->updateByCond($map, $cond, false);
		return $res;
	}
//=========================================权限角色=============================================================
	/** 获取指定角色记录 */
	public function getRoleItem($cols, $cond){
		return $this->roleDao->selectRowByCond($cols, $cond);
	}
	/** 根据条件获取角色列表 */
	public function getRoleList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->roleDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->roleDao->getCountByCond($cond);
		}
		return $res;
	}
	/** 添加角色记录 */
	public function addRoleItem($data){
		$map = Tin :: createDataMap();
		$map->addEntry('ROLE_NAME', $data['role_name'], DB::VARCHAR);
		$map->addEntry('STATE', $data['state'], DB::INT);
		//$map->addEntry('GEO_CODE', $data['geo_code'], DB::INT);
		$map->addEntry('CREATE_TIME', date("Y-m-d H:i:s"), DB::DATETIME);
		try{
			$res = $this->roleDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
            $this->logger->error("addRoleItem:".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 更新指定角色记录 */
	public function updateRoleItem($data, $cond){
		$map = Tin :: createDataMap();
		$map->addEntry('ROLE_NAME', $data['role_name'], DB::VARCHAR);
		$map->addEntry('STATE', $data['state'], DB::INT);
		//$map->addEntry('GEO_CODE', $data['geo_code'], DB::INT);
		try{
			$res = $this->roleDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
            $this->logger->error("updateRoleItem".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 删除指定角色记录 */
	public function delRoleItem($cond, $params = false){
		return $this->roleDao->deleteByCond($cond, $params);
	}
//=========================================权限=============================================================
	/** 根据条件获取权限数组 */
	public function getPermissions($cols, $cond){
		$permissList = $this->permissionDao->selectAll($cols, $cond);
		$res = array();
		$res['menuids'] = '0';
		$res['functionids'] = '0';
		foreach($permissList as $k=>$v){
			$res['menuids'] .= ','.$v['menu_id'];
			if($v['function_ids'])
				$res['functionids'] .= ','.$v['function_ids'];
		}
		return $res;
	}

}//End Class
?>
